package thread;


import javax.xml.transform.Source;

public class Thread_interrupt {

    public static void main(String[] args) {
        Thread working = new Thread() {
            @Override
            public void run() {
                while (!Thread.currentThread().isInterrupted()) {
                    System.out.println("working");
                    try {
                        Thread.sleep(1000);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                        //在中断前处理的业务
                        System.out.println("做工作交接...");
                        //结束任务了
                        System.out.println("结束工作");
                        return;
                    }
                }
            }
        };
        working.start();
        //让线程工作5秒
        try {
            Thread.sleep(5000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        //通知线程该结束了
        System.out.println("通知线程可以结束工作了！");
        working.interrupt();
    }
}
